/**
 * Created by chenyong on 15/11/16.
 * 超级像素全景对象
 * 所有的全景图皆由此对象创建，并生成一个document.bpsviewer的全景变量
 *
 *  bpsviewer=new BigPixelViewer("<%= tourxmlpath%>","swift1989");
 *  document.bpsviewer=bpsviewer;
 *  bpsviewer.embed();;
 */

var BigPixelViewer=function(xml,panoId){

    var lastFov;
    var lastHlookat;
    var lastVlookat;

    //当视图发生改变时触发的事件
    var _onviewerChangEvent;

    var self=this;
    this.krpano=null;
    this.isReady=false;
    this.panoId = panoId;

    this.krpanoReady=function(krpano){

        //console.log(BrowserDetect.isMobile);
        if(BrowserDetect.isMobile)
        {
            var _stagescale =document.documentElement.clientWidth /320 /2;
            krpano.set("stagescale", _stagescale);
        }

        if(self!=null&&krpano!=null)
            self.krpano=krpano;
        if(self==null||self.krpano==null)
            return;
        self.waitPano(function()
        {
            self.isReady=true;
            BpsViewerReady();
        });
    };

    this.waitPano=function(callback){
        var isReady=false;
        self.call("addhotspot(ngrunning)");
        try{
            var test=self.get("hotspot[ngrunning]");
            if(test!=null&&test.sprite!=null)
            {
                isReady=true;
            }
        }
        catch(e)
        {isReady=false;}
        if(isReady){if(!self.isReady){self.call("removehotspot(ngrunning)");callback();}}else setTimeout(function(){self.waitPano(callback);},10);
    };

    var html5="prefer";

    if(BrowserDetect.browser=="Explorer"&&BrowserDetect.version<=10)
        html5="never";
    else if(BrowserDetect.browser=="Explorer")
        html5="prefer+css3d";
    this.viewer=createPanoViewer({swf:"http://cdn.bigpixel.cn/libs/krpano/1.18.5/tour.swf",js:"http://cdn.bigpixel.cn/libs/krpano/1.18.5/tour.js",xml:xml,target:"pano-container",html5:html5,passQueryParameters:false,onready:this.krpanoReady,wmode:"opaque"});

    this.call=function(action){this.krpano.call(action);};
    this.set=function(variable,value){this.krpano.set(variable,value);};
    this.get=function(variable){return this.krpano.get(variable);};
    this.embed=function(){this.viewer.embed();};
    this.zoomInDown=function(){this.set('fov_moveforce',-0.75);};
    this.zoomOutDown=function(){this.set('fov_moveforce',+0.75);};
    this.zoomUp=function(){this.set('fov_moveforce',0);};
    this.moveLeftDown=function(){this.set('hlookat_moveforce',-0.75);};
    this.moveRightDown=function(){this.set('hlookat_moveforce',+0.75);};
    this.moveTopDown=function(){this.set('vlookat_moveforce',-0.75);};
    this.moveBottomDown=function(){this.set('vlookat_moveforce',+0.75);};
    this.moveUp=function(){this.set('vlookat_moveforce',0);this.set('hlookat_moveforce',0);};

    function BpsViewerReady(){
        lastFov=self.krpano.get('view.fov');
        lastHlookat=self.krpano.get('view.hlookat');
        lastVlookat=self.krpano.get('view.vlookat');
        self.krpano.set('events.onviewchanged','js(bpsviewer.BpsViewerChanged(););');
    }

    this.BpsViewerChanged = function(){
        var fov=self.krpano.get('view.fov');
        var hlookat=self.krpano.get('view.hlookat');
        var vlookat=self.krpano.get('view.vlookat');

        if(Math.abs(lastFov-fov)>fov/50||Math.abs(lastHlookat-hlookat)>1||Math.abs(lastVlookat-vlookat)>1)
        {
            lastFov=fov;
            lastHlookat=hlookat;
            lastVlookat=vlookat;
            var args  = Array.prototype.slice.call(arguments);
            if(typeof(_onviewerChangEvent) == "function" )
            {
                _onviewerChangEvent.apply(null,args);
            }
        }
    }

    this.OnBpsViewerChanged = function(func)
    {
        _onviewerChangEvent = func;
    }

    //加载
    if(typeof UserTags=='function')
    {
        console.log(typeof UserTags);
        this.UserTags = new UserTags(this.panoId);
    }

}